﻿using LiveLinkImporter.Data;
using Futajma.MigrationStudio;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Futajma.MigrationStudio.LiveLinkImporter;

namespace LiveLinkImporter.Dialog
{
    public partial class DeltaUpgradeDialog : Form
    {
        LiveLinkFileImporter _importer = null;
        TreeView _sourceTree, _targetTree;
        int _currentVersion;
        Dictionary<string, UpdateAnalysisItem> _updateAnalysisItem;
        BindingSource _bindingSource = new BindingSource();

        public DeltaUpgradeDialog(LiveLinkFileImporter importer, TreeView sourceTree, TreeView targetTree, int currentVersion)
        {
            _importer = importer;
            _sourceTree = sourceTree;
            _targetTree = targetTree;
            _currentVersion = currentVersion;
            _updateAnalysisItem = new Dictionary<string, UpdateAnalysisItem>();
            InitializeComponent();

            dgwResults.AutoGenerateColumns = false;
            var cbxCol = dgwResults.Columns["UpdateAction"] as DataGridViewComboBoxColumn;
            cbxCol.DataSource = Enum.GetValues(typeof(UpdateAction));
            cbxCol.ValueType = typeof(UpdateAction);

            dgwResults.CellFormatting += dgwResults_CellFormatting;
            //_bindingSource.DataSource = _updateAnalysisItem;
            dgwResults.DataSource = _bindingSource;

        }

        void dgwResults_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dgwResults.Columns[e.ColumnIndex].Name == "Status")
            {

                var item = dgwResults.Rows[e.RowIndex].DataBoundItem as UpdateAnalysisItem;

                if (item != null)
                {

                    var cbx = dgwResults.Rows[e.RowIndex].Cells["UpdateAction"] as DataGridViewComboBoxCell;

                    switch (item.AnalysisState)
                    {
                        case Data.AnalysisState.FoundInTarget:
                            e.Value = Icons.StatusAnnotations_Complete_and_ok_16xLG_color;
                            //AssignValuesToComboBox(cbx, Data.AnalysisState.FoundInTarget);
                            break;
                        case Data.AnalysisState.FoundInTargetDifferentFileName:
                            e.Value = Icons.Error_red_16x16;
                            cbx.ReadOnly = false;
                            //AssignValuesToComboBox(cbx, Data.AnalysisState.FoundInTargetDifferentFileName);
                            break;
                        case Data.AnalysisState.NewCreatedInSource:
                            e.Value = Icons.warning_yellow_7231_12x12_16x;
                            //AssignValuesToComboBox(cbx, Data.AnalysisState.NewCreatedInSource);
                            break;
                        case Data.AnalysisState.NotMapped:
                            e.Value = Icons.FeatureNotAvailable_5734;
                            //AssignValuesToComboBox(cbx, Data.AnalysisState.NotMapped);
                            break;
                        case Data.AnalysisState.NotMappedDifferentFileName:
                            e.Value = Icons.FeatureNotAvailable_5734;
                            //AssignValuesToComboBox(cbx, Data.AnalysisState.NotMappedDifferentFileName);
                            break;

                    }
                }
            }
        }

        // not longer used ?
        private void AssignValuesToComboBox(DataGridViewComboBoxCell cbx, Data.AnalysisState analysisState)
        {
            if (cbx != null && cbx.Items.Count <= 0)
            {
                switch (analysisState)
                {
                    case Data.AnalysisState.FoundInTarget:
                        cbx.Items.Add("None");
                        break;
                    case Data.AnalysisState.FoundInTargetDifferentFileName:
                        cbx.Items.Add("None");
                        cbx.Items.Add("MoveToSource");
                        cbx.Items.Add("RenameFile");
                        break;
                    case Data.AnalysisState.NewCreatedInSource:
                        cbx.Items.Add("None");
                        break;
                    case Data.AnalysisState.NotMapped:
                        cbx.Items.Add("None");                        
                        break;
                    case Data.AnalysisState.NotMappedDifferentFileName:
                        cbx.Items.Add("None");
                        break;
                }
            }
        }

        private void btnStartAnalysis_Click(object sender, EventArgs e)
        {
            _updateAnalysisItem.Clear();
            //_importer.DeltaUpdateAnalysis(_updateAnalysisItem, _sourceTree, _targetTree);
            _bindingSource.DataSource = _updateAnalysisItem.Values;
            _bindingSource.ResetBindings(true);
        }

        private void btnExportReport_Click(object sender, EventArgs e)
        {

        }

        private void btnApproveMigration_Click(object sender, EventArgs e)
        {

        }
    }
}
